#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "data.h"

struct sdata {
	char* s;
	int tam;
};

data data_fromString (char* s) {
	data d = (data)calloc(1,sizeof(struct sdata));

	if  (d != NULL) {
		d->s = (char*)calloc(strlen(s)+1,sizeof(char));
		d->s = strcpy(d->s,s);
		d->tam = strlen(s);
	}

	return d;
}

char* data_toString (data d) {
	char * str = NULL;

	if (d != NULL) {
		if (d->s != NULL) {
			str = (char*)calloc(d->tam+1,sizeof(char));
			str = strcpy(str,d->s);
		}
	}

	return str;
}

int data_lenght (data d) {
	return (d->tam);
}

data data_clone (data d) {
	data clon = data_fromString(d->s);

	return clon;
}

data data_destroy (data d) {
	if (d != NULL) {
		if(d->s != NULL) {
			free(d->s);
			d->s = NULL;
		}
		free(d);
		d = NULL;
	}

	return d;
}
